using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using P4COM;

/*
********************************************************************************
'
' Copyright (c) 2003, Robert Cowham and Vaccaperna Systems Ltd.  All rights reserved.
'
' Redistribution and use in source and binary forms, with or without
' modification, are permitted provided that the following conditions are met:
'
' 1.  Redistributions of source code must retain the above copyright
'     notice, this list of conditions and the following disclaimer.
'
' 2.  Redistributions in binary form must reproduce the above copyright
'     notice, this list of conditions and the following disclaimer in the
'     documentation and/or other materials provided with the distribution.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
' IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
' ARE DISCLAIMED. IN NO EVENT SHALL VACCAPERNA SYSTEMS LTD. BE LIABLE FOR ANY
' DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
' (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
' LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
' ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
' (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
' SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'
' *******************************************************************************
'
' Name:		main.cs
'
' Author:   Robert Cowham <robert@vaccaperna.co.uk>
'
' Description:
'   Very simple demo of p4com - COM interface to Perforce.
'
'   Executes any command which the users types in txtCmd and puts the results
'   into the ResultBox.
'
'   Please note that there is no real error handling.
' ******************************************************************************
*/
namespace p4comtest
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txtCmd;
		private System.Windows.Forms.ListBox ResultBox;
		private System.Windows.Forms.Button cmdRun;
		private System.Windows.Forms.Button cmdEnv;
		private System.Windows.Forms.Button cmdSubmit;
		private System.Windows.Forms.Button cmdClose;
		private System.Windows.Forms.Button cmdClear;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public MainForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.txtCmd = new System.Windows.Forms.TextBox();
			this.ResultBox = new System.Windows.Forms.ListBox();
			this.cmdRun = new System.Windows.Forms.Button();
			this.cmdEnv = new System.Windows.Forms.Button();
			this.cmdSubmit = new System.Windows.Forms.Button();
			this.cmdClose = new System.Windows.Forms.Button();
			this.cmdClear = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(64, 24);
			this.label1.TabIndex = 0;
			this.label1.Text = "Cmd:";
			// 
			// txtCmd
			// 
			this.txtCmd.Location = new System.Drawing.Point(112, 24);
			this.txtCmd.Name = "txtCmd";
			this.txtCmd.Size = new System.Drawing.Size(408, 20);
			this.txtCmd.TabIndex = 1;
			this.txtCmd.Text = "info";
			// 
			// ResultBox
			// 
			this.ResultBox.Location = new System.Drawing.Point(112, 48);
			this.ResultBox.Name = "ResultBox";
			this.ResultBox.Size = new System.Drawing.Size(400, 225);
			this.ResultBox.TabIndex = 2;
			// 
			// cmdRun
			// 
			this.cmdRun.Location = new System.Drawing.Point(120, 296);
			this.cmdRun.Name = "cmdRun";
			this.cmdRun.Size = new System.Drawing.Size(88, 32);
			this.cmdRun.TabIndex = 3;
			this.cmdRun.Text = "&Run";
			this.cmdRun.Click += new System.EventHandler(this.cmdRun_Click);
			// 
			// cmdEnv
			// 
			this.cmdEnv.Location = new System.Drawing.Point(216, 296);
			this.cmdEnv.Name = "cmdEnv";
			this.cmdEnv.Size = new System.Drawing.Size(88, 32);
			this.cmdEnv.TabIndex = 4;
			this.cmdEnv.Text = "&Env";
			this.cmdEnv.Click += new System.EventHandler(this.cmdEnv_Click);
			// 
			// cmdSubmit
			// 
			this.cmdSubmit.Location = new System.Drawing.Point(312, 296);
			this.cmdSubmit.Name = "cmdSubmit";
			this.cmdSubmit.Size = new System.Drawing.Size(88, 32);
			this.cmdSubmit.TabIndex = 5;
			this.cmdSubmit.Text = "&Submit";
			this.cmdSubmit.Click += new System.EventHandler(this.cmdSubmit_Click);
			// 
			// cmdClose
			// 
			this.cmdClose.Location = new System.Drawing.Point(408, 296);
			this.cmdClose.Name = "cmdClose";
			this.cmdClose.Size = new System.Drawing.Size(88, 32);
			this.cmdClose.TabIndex = 6;
			this.cmdClose.Text = "&Close";
			this.cmdClose.Click += new System.EventHandler(this.cmdClose_Click);
			// 
			// cmdClear
			// 
			this.cmdClear.Location = new System.Drawing.Point(8, 72);
			this.cmdClear.Name = "cmdClear";
			this.cmdClear.Size = new System.Drawing.Size(88, 32);
			this.cmdClear.TabIndex = 7;
			this.cmdClear.Text = "C&lear";
			this.cmdClear.Click += new System.EventHandler(this.cmdClear_Click);
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(528, 358);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.cmdClear,
																		  this.cmdClose,
																		  this.cmdSubmit,
																		  this.cmdEnv,
																		  this.cmdRun,
																		  this.ResultBox,
																		  this.txtCmd,
																		  this.label1});
			this.Name = "MainForm";
			this.Text = "p4com C# Demo Application";
			this.ResumeLayout(false);

		}
		#endregion

		P4COM.p4 m_p4 = new P4COM.p4();
		System.Array output;
		System.Array warnings;
		System.Array errors;


		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new MainForm());
		}

		private void cmdRun_Click(object sender, System.EventArgs e)
		{
			m_p4.Connect();
			m_p4.ExceptionLevel = 1;
			try
			{
				output = m_p4.run(txtCmd.Text);
			}
			catch (System.Runtime.InteropServices.COMException ex)
			{
				MessageBox.Show(ex.Message);
			}
			DisplayResult();

            int d = m_p4.Dropped();
            if (d != 0)
            {
                MessageBox.Show("connection dropped");
            }
			m_p4.Disconnect();
		}
		private void DisplayResult()
		{
			string s;
			s = "Cmd:" + txtCmd.Text;
			ResultBox.Items.Add(s);
			warnings = m_p4.Warnings;
			errors = m_p4.Errors;
			DisplayArrayOpt("Output:", output);
			DisplayArrayOpt("Warnings:", warnings);
			DisplayArrayOpt("Errors:", warnings);
			if (m_p4.TempFilename != null && m_p4.TempFilename.Length > 0)
			{
				s = "TempFilename:";
				s += m_p4.TempFilename;
				ResultBox.Items.Add(s);
			}
			ResultBox.Items.Add(" ");
		}

		private void DisplayArrayOpt(string Msg, System.Array arr)
		{
			if (arr.GetUpperBound(0) >= arr.GetLowerBound(0))
			{
				DisplayArray(Msg, arr);
			}
		}

		private void DisplayArray(string Msg, System.Array arr)
		{
			int i;
			ResultBox.Items.Add(Msg);
			for (i = 0; i < arr.Length; i++)
			{
				ResultBox.Items.Add(arr.GetValue(i));
			}
		}

		private void cmdClear_Click(object sender, System.EventArgs e)
		{
			ResultBox.Items.Clear();
		}

		private void cmdEnv_Click(object sender, System.EventArgs e)
		{
			EnvForm f = new EnvForm();
			f.DoEnv(m_p4);
		}

		private void cmdClose_Click(object sender, System.EventArgs e)
		{
			Close();
		}

		private void cmdSubmit_Click(object sender, System.EventArgs e)
		{
			SubmitForm f = new SubmitForm();
			f.DoSubmit(m_p4);
		}
	}
}
